package com.zhuchaooasystem.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @author yinsi
 *         <p/>
 *         排序工具类
 */
public class SortUtils {

    /**
     * 排序
     *
     * @param mHashMap
     * @return
     */
    public static String sort(Map<String, String> mHashMap) {
        String sortStr = "";
        try {
            List<Map.Entry<String, String>> mHashMapEntryList = new ArrayList<Map.Entry<String, String>>(mHashMap.entrySet());
            Collections.sort(mHashMapEntryList, new Comparator<Map.Entry<String, String>>() {

                @Override
                public int compare(Map.Entry<String, String> firstMapEntry,
                                   Map.Entry<String, String> secondMapEntry) {
                    return firstMapEntry.getKey().compareTo(secondMapEntry.getKey());
                }
            });
            sortStr = contactKeyValue(mHashMapEntryList, "&");
        } catch (Exception e) {
        }
        return sortStr;
    }

    /**
     * 字符串拼接
     *
     * @param mHashMapEntryList
     * @param split
     * @return
     */
    public static String contactKeyValue(List<Map.Entry<String, String>> mHashMapEntryList, String split) {
        String sortStr = "";
        try {
            if (null != mHashMapEntryList && mHashMapEntryList.size() > 0) {
                for (int i = 0; i < mHashMapEntryList.size(); i++) {
                    sortStr += mHashMapEntryList.get(i).getKey() + "=" + mHashMapEntryList.get(i).getValue();
                    if (i < mHashMapEntryList.size() - 1) {
                        sortStr += split;
                    }
                }
            }
        } catch (Exception e) {
        }
        return sortStr;
    }


}
